package com.easy.component.utils;

import android.os.Environment;
import android.os.StatFs;

import java.io.File;

/**
 * 描述: {描述}
 * 作者:
 * 创建日期: 2017/11/2 10:15
 * 修改日期: 2017/11/2
 * 版本: {版本}
 */
public class SDCardUtils {
    private SDCardUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static boolean isSDCardEnable() {
        return Environment.getExternalStorageState().equals("mounted");
    }

    public static String getSDCardPath() {
        return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
    }

    public static long getSDCardAllSize() {
        if(isSDCardEnable()) {
            StatFs stat = new StatFs(getSDCardPath());
            long availableBlocks = (long)stat.getAvailableBlocks() - 4L;
            long freeBlocks = (long)stat.getAvailableBlocks();
            return freeBlocks * availableBlocks;
        } else {
            return 0L;
        }
    }

    public static long getFreeBytes(String filePath) {
        if(filePath.startsWith(getSDCardPath())) {
            filePath = getSDCardPath();
        } else {
            filePath = Environment.getDataDirectory().getAbsolutePath();
        }

        StatFs stat = new StatFs(filePath);
        long availableBlocks = (long)stat.getAvailableBlocks() - 4L;
        return (long)stat.getBlockSize() * availableBlocks;
    }

    public static String getRootDirectoryPath() {
        return Environment.getRootDirectory().getAbsolutePath();
    }
}

